package com.kung.qvs.ui.main.fragment.manage;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.Navigation;

import com.kung.qvs.BR;
import com.kung.qvs.R;
import com.kung.qvs.app.AppViewModelFactory;
import com.kung.qvs.databinding.FragWorkerListBinding;
import com.kung.qvs.entity.ProductBean;
import com.kung.qvs.entity.WorkerBean;
import com.kung.qvs.ui.main.KungViewModel;
import com.kung.qvs.ui.widget.ConformDialog;

import me.goldze.mvvmhabit.base.BaseFragment;

public class FragWorkers extends BaseFragment<FragWorkerListBinding, FragWorkersModel> {
    private KungViewModel kungViewModel;
    private boolean isSelect = false;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.frag_worker_list;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public FragWorkersModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(getActivity(), factory).get(FragWorkersModel.class);
    }

    @Override
    public void initParam() {
        super.initParam();
        if (getArguments() != null) {
            isSelect = getArguments().getBoolean("isSelect", false);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        viewModel.funLoadWorkers();
    }

    @Override
    public void initData() {
        super.initData();
        kungViewModel = new ViewModelProvider(getActivity()).get(KungViewModel.class);
        viewModel.isSelect.set(isSelect);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        //返回上个页面
        viewModel.uc.eventToLastPage.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                //关闭页面，返回首页
                Navigation.findNavController(getView()).navigateUp();
            }
        });
        //编辑产品
        viewModel.uc.eventEditWorker.observe(this, new Observer<WorkerBean>() {
            @Override
            public void onChanged(WorkerBean workerBean) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("worker", workerBean);
                Navigation.findNavController(getView()).navigate(R.id.fragWorkerAdd, bundle);
            }
        });
        //新增产品
        viewModel.uc.eventAddWorker.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                Navigation.findNavController(getView()).navigate(R.id.fragWorkerAdd);
            }
        });
        //删除产品
        viewModel.uc.eventDeleteWorker.observe(this, new Observer<WorkerBean>() {
            @Override
            public void onChanged(WorkerBean workerBean) {
                ConformDialog.Builder builder = new ConformDialog.Builder(getActivity());
                builder.setMessage("是否删除'" + workerBean.getNickName() + "'？", "");
                builder.setTitle("删除产品");
                builder.setPositiveButton("确认",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog2,
                                                int which) {
                                dialog2.dismiss();
                                viewModel.funDeleteWorker(workerBean);
                            }
                        });

                builder.setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog2,
                                                int which) {
                                dialog2.dismiss();
                            }
                        });
                builder.create().show();

            }
        });

        //选择工人
        viewModel.uc.eventSelectWorker.observe(this, new Observer<WorkerBean>() {
            @Override
            public void onChanged(WorkerBean workerBean) {
                kungViewModel.setmSelectWorker(workerBean);
                Navigation.findNavController(getView()).navigateUp();
            }
        });
    }
}
